package com.example.netchat;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class CreateRoomDialogFragment extends DialogFragment {
	private EditText mRoomNameTextView;
	private static final String ROOM_NAME_STATE_KEY = "NICK";

	public View createView(LayoutInflater inflater, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_create_room, null);

		mRoomNameTextView = (EditText) view.findViewById(R.id.nick_edittext);

		return view;
	}

	@Override
	public void onSaveInstanceState(Bundle bundle) {
		bundle.putString(ROOM_NAME_STATE_KEY, mRoomNameTextView.getText()
				.toString());

		super.onSaveInstanceState(bundle);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = super.onCreateDialog(savedInstanceState);
		dialog.setTitle(R.string.add_room);

		View view = createView(getActivity().getLayoutInflater(), null);

		if (savedInstanceState != null) {
			String roomName = savedInstanceState.getString(ROOM_NAME_STATE_KEY);

			if (roomName == null) {
				roomName = "";
			}

			mRoomNameTextView.setText(roomName);
		}

		return new AlertDialog.Builder(getActivity()).setCancelable(false)
				.setIcon(R.drawable.ic_launcher).setTitle(R.string.add_room)
				.setView(view)
				.setPositiveButton(R.string.add_room, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						((CreateRoomDialogListener) getActivity())
								.onCreateRoomDialogConfirmed(mRoomNameTextView
										.getText().toString());
						dialog.dismiss();

					}
				}).setNegativeButton(R.string.cancel, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).create();
	}
}
